<?php
/**
 * Created by PhpStorm.
 * User: songyichao
 * Date: 2017/09/20
 * Time: 08:34
 */
namespace  backend\models;

use backend\utils\ImageSize;
use common\base_models\kds\AgentArea;
use yii\base\Model;
use backend\base\BaseModel;

class UserModel extends BaseModel
{
    private static $_instances = array();
    public static $registered_from_arr = array("0"=>'pc网站',1=>'手机网站',2=>'微信网站',3=>'安卓',4=>'苹果',5=>'微信小程序');
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['nickname'],'required','on'=>'edit','message'=>'{attribute}不能为空'],
            [['id','class','vip','is_delete','update_time','create_time','education_subject'], 'integer'],
            [['telephone'], 'match', 'pattern'=>'/^\d{8,12}$/', 'message'=>'{attribute}格式错误！'],
            [['autograph','icon'], 'string', 'max' => 100,'message'=>'{attribute}不能超过100字符'],
            [['birthday'], 'match', 'pattern'=>'/^\d{4}\-\d{1,2}\-\d{1,2}$/','message'=>'{attribute}格式错误'],
            [['nickname'], 'string', 'max' => 20,'message'=>'{attribute}不能超过20字符'],
            [['telephone'], 'unique']
        ];
    }
    public function scenarios()
    {
        return array_merge(parent::scenarios(),[
            'edit' => ['nickname', 'province','city','country','birthday','autograph','education_subject'],
        ]);
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'nickname' => '昵称',
            'class' => '类型',
            'icon' => '头像',
            'telephone' => '手机号',
            'birthday' => '生日',
            'autograph' => '个性签名',
            'create_time' => '注册时间',
            'update_time' => '更新时间',
        ];
    }

    public function getIconImg(){
        if(!$this->icon) return '';
        return '<img src="'.ImageSize::resize($this->icon,100,100).'" style="width:100px;height:100px;" alt="">';
    }

    public static function resetTelephone($tel){
        $user = \Yii::$app->params['user'];
        $user_id = \Yii::$app->user->id;
        if($tel) {
            if (!in_array($user_id, $user)) {
                $tel = substr($tel, 0, 3) . '****' . substr($tel, 7, 4);
            } else {
                $tel = $tel;
            }
        }else{
            $tel = '';
        }
        return $tel;
    }

    /**
     * 单例初始化model
     * @param string $className
     * @return mixed
     */
    public static function getInstance($className = __CLASS__ )
    {
        if (empty(self::$_instances[$className])) {
            self::$_instances[$className] = new $className(null, null, []);
        }
        return self::$_instances[$className];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
                $this->is_delete = 0;
            }else{
                //不允许修改手机号
                if($this->telephone != $this->getOldAttribute('telephone')){
                    $this->telephone = $this->getOldAttribute('telephone');
                }
            }
            $this->update_time = $time;
            $_area = \Yii::$app->request->post('_area');
            if($_area){
                if(isset($_area['province'])) $this->province = $_area['province'];
                if(isset($_area['city'])) $this->city = $_area['city'];
                if(isset($_area['country'])) $this->country = $_area['country'];
            }
            return true;
        }
        return false;
    }

}
